/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.sapra.ServletsPoliza;

import java.io.IOException;
import java.io.PrintWriter;
import java.text.DecimalFormat;
import java.text.SimpleDateFormat;
import java.util.List;
import java.util.Locale;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.sapra.Prefactura.PrefacturaMO;
import org.sapra.dao.Empresa;
import org.sapra.dao.Periodo;
import org.sapra.dep.Practicante;
import org.sapra.dep.conexion.DBWRAP;
import org.sapra.logon.dLogin;

/**
 *
 * @author luiz
 */
public class ServletPoliza extends HttpServlet {	@Override
	public void init() throws ServletException {
		// TODO Auto-generated method stub
		super.init();
	//	redirectPage =getInitParameter("redirectPage");

	}
	@Override
	protected void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doTask(req,resp);
	}

	@Override
	protected void doPost(HttpServletRequest req, HttpServletResponse resp)
			throws ServletException, IOException {
		// TODO Auto-generated method stub
		doTask(req,resp);
	}

	private void doTask(HttpServletRequest request, HttpServletResponse response){

            DBWRAP pdb=null;
            try {
            pdb = new DBWRAP();
            String _speriodo = (String) request.getSession().getAttribute("_idperiodo");
            int periodo = Integer.parseInt(_speriodo);
            //Respuesta
            PrintWriter out = response.getWriter();
             PrefacturaMO dm = new PrefacturaMO();
             Periodo pe = dm.getPeriodo(periodo);
             Empresa emp=dLogin.getEmpresa2(pe.empresa);
             dm.InitFactura(emp, pe);

             List<Practicante> m_pract=dm.getM_practicantes();
             DecimalFormat nf = new DecimalFormat("## ### ###.00");
             SimpleDateFormat df = new SimpleDateFormat("yyyy-MMM-dd",new Locale("es"));
             String file1="";
                         file1+="<html>"
                    + " <head><title>Facturacion  "+pe.toString()+"</title></head>"
                    + "<body>";
            file1+="<img src=\"http://www.administraciondepracticantes.com/imagenes/logo.JPG\" alt=\"CAINTRA NL\" /><br>";
            file1+="<table border=1 cellspacing=1 cellpadding=0>"
                            + " <tr>"
                                    +"<td>Empresa</td>"
                                    +"<td>"+emp.getRazon_empresa()+"</td>"
                            +"</tr>"
                            +"<tr>"
                                    +"<td>Razon Factura</td>"
                                    +"<td>"+emp.getRazon_factura()+"</td>"
                            +"</tr>"
                            +"<tr>"
                                    +"<td>Periodo</td>"
                                    +"<td>"+pe.toString()+"</td>"
                            +"</tr>"
                      + "</table><br><br>";
            file1+="<table border=1 cellspacing=1 cellpadding=0>";
             String[] titulo={"#","CUENTA","NETO","NOMBRE COMPLETO","CCOSTO","DEPARTAMENTO","FECHA DE INICIO","FECHA DE TERMINO",
                     "FECHA BAJA","APORTACION MENSUAL","DIAS A PAGAR","AJUSTES",
                     "SEGURO EMPRESA","SEGURO PRACTICANTE","DEDUCCIONES","MONTO A PAGAR","GA","SUBTOTAL","IVA","TOTAL PRACTICANTE"};
             for(int i=0;i<titulo.length;i++){
                    file1+="<th>"+titulo[i]+"</th>";
             }
             double foot_neto=0;
             double foot_segEmp=0;
             double foot_ajuste=0;
             double foot_setPrac=0;
             double foot_deduc=0;
             double foot_mont_pagar=0;
             double foot_ga=0;
             double foot_subtot=0;
             double foot_iva=0;
             double foot_totalp=0;
             dm.getTotales();
            for(int i=0;i<m_pract.size();i++){
                Practicante p = (Practicante)m_pract.get(i);
                file1+="<tr>";
                        file1+="<td>"+(i+1)+"</td>";

                        file1+="<td>";
                            if(p.cuenta==null)
                                file1+=" N/D ";
                            else
                                 file1+=p.cuenta;
                        file1+="</td>";

                        file1+="<td>"+nf.format(p.neto)+"</td>";
                        foot_neto+=p.neto;
                        file1+="<td>"+p.nombre+" "+p.paterno+" "+p.materno+"</td>";


                        file1+="<td>";
                                 if(p.centro==null || p.centro.trim().equals(""))
                                    file1+=" N/D ";
                                else
                                    file1+=p.centro;
                        file1+="</td>";

                         file1+="<td>"+p.departamento+"</td>";
                         file1+="<td>"+df.format(p.fechaInicio)+"</td>";
                         file1+="<td>"+df.format(p.fechaTermino)+"</td>";

                        file1+="<td>";
                        if(p.fechaCancelacion!=null)
                            file1+=df.format(p.fechaCancelacion);
                        else
                            file1+="N/D";
                        file1+="</td>";

                        file1+="<td>"+nf.format(p.sueldo)+"</td>";
                        file1+="<td>"+p.dias+"</td>";
                        file1+="<td>"+nf.format(p.ajuste)+"</td>";
                        foot_ajuste+=p.ajuste;
                        
                        file1+="<td>"+nf.format(p.polizaEmpresa)+"</td>";
                        foot_segEmp+=p.polizaEmpresa;
                        file1+="<td>"+nf.format(p.polizaPracticante)+"</td>";
                        foot_setPrac+=p.polizaPracticante;
                        file1+="<td>"+nf.format(p.deduccion)+"</td>";
                        foot_deduc+=p.deduccion;
                        //viene lo demas

                        file1+="<td>"+nf.format(p.apagar)+"</td>";//monto a apgar
                        foot_mont_pagar+=p.apagar;
                        double ga=p.apagar*dm.getComision();//Gasto Administrativo
                        foot_ga+=ga;
                        file1+="<td>"+nf.format(ga)+"</td>";//fin Gasto Administrativo
                        double subt_w=p.apagar+ga;//subtotal
                        foot_subtot+=subt_w;
                        file1+="<td>"+nf.format(subt_w)+"</td>";//subtotal

                        double iva_w=subt_w * .16;
                        foot_iva+=iva_w;
                        file1+="<td>"+nf.format(iva_w)+"</td>";//iva

                        double total_w=subt_w+iva_w;
                        foot_totalp+=total_w;

                        file1+="<td>"+nf.format(total_w)+"</td>";//total
                file1+="</tr>";

            }
             file1+="<tr>";
                file1+="<td>&nbsp;</td><td>&nbsp;</td>";
                file1+="<td>"+nf.format(foot_neto)+"</td>";
                file1+="<td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td><td>&nbsp;</td>";
                file1+="<td>"+nf.format(foot_ajuste)+"</td>";
                file1+="<td>"+nf.format(foot_segEmp)+"</td>";
                file1+="<td>"+nf.format(foot_setPrac)+"</td>";
                file1+="<td>"+nf.format(foot_deduc)+"</td>";
                file1+="<td>"+nf.format(foot_mont_pagar)+"</td>";
                file1+="<td>"+nf.format(foot_ga)+"</td>";
                file1+="<td>"+nf.format(foot_subtot)+"</td>";
                file1+="<td>"+nf.format(foot_iva)+"</td>";
                file1+="<td>"+nf.format(foot_totalp)+"</td>";
             file1+="</tr>";
            file1+="</table><br>";
            
            double subTotFin=dm.getTotal1()-dm.getAbonoTotal();
            file1+="<table border=1 cellspacing=1 cellpadding=0>";

            file1+="  <th> Concepto </th><th>  Monto </th> ";
            file1+=" <tr><td> Monto a Pagar </td><td> $"+nf.format(dm.getTotalApagar())+"</td></tr>";
            file1+=" <tr><td> "+dm.getComision()*100+" % G.A </td><td> $ "+nf.format(dm.getAdmGasto())+"</td></tr>";
            file1+=" <tr><td> SubTotal Sin Seguro </td><td> $ "+nf.format(dm.getSubTotalSS())+"</td></tr>";
            file1+=" <tr><td> Seguro Empresa </td><td> $ "+nf.format(dm.getTotalSeguroEmpresa())+"</td></tr>";
            file1+=" <tr><td> Adeudos Empresa </td><td> $ "+nf.format(dm.getAdeudoTotal())+"</td></tr>";
            file1+=" <tr><td> SubTotal con Seguro </td><td> $ "+nf.format(dm.getM_SubTotal())+"</td></tr>";
            file1+=" <tr><td> I.V.A </td><td> $ "+nf.format(dm.getIva())+"</td></tr>";
            file1+=" <tr><td> Total1 </td><td> $ "+nf.format(dm.getTotal1())+"</td></tr>";
            file1+=" <tr><td> Abono Empresa </td><td> $ "+nf.format(dm.getAbonoTotal())+"</td></tr>";
            file1+=" <tr><td> SubTotal con Abonos </td><td> $ "+nf.format(subTotFin/1.16)+"</td></tr>";
            file1+=" <tr><td> I.V.A. </td><td> $ "+nf.format((subTotFin/1.16)*.16)+"</td></tr>";
            file1+=" <tr><td> Total Final </td><td> $ "+nf.format(subTotFin)+"</td></tr>";
            file1+="</table>";
            file1+="</body>"
                    + " </html>";
			out.println(file1);
			out.flush();
                        pdb.close();
                        pdb=null;
	}catch(Exception e){
            e.printStackTrace();
            pdb.close();
                        pdb=null;
        }finally{
                         pdb.close();
                        pdb=null;
        }
    }

}
